import {defineStore} from "pinia";
import {computed, ref} from "vue";
import {initDynamicRoutes} from "@/router/dynamicRouters";
import {getNav} from "@/api/web/user.js";
import router from "@/router/index.js";


export const useTokenStore = defineStore("nova-token",()=>{
    const novaToken = ref('null')
    const setToken = (v) => {
        novaToken.value = v
        localStorage.setItem("nova-token", v)
    }

    const removeToken = ()=>{
        localStorage.removeItem("nova-token")
        novaToken.value = null
    }

    const getToken = () => {

        if(null!==novaToken.value&&"null"!==novaToken.value){
            return novaToken.value
        }else {
            if(null!==localStorage.getItem("nova-token")&&"null"!==localStorage.getItem("nova-token")){
                setToken(localStorage.getItem("nova-token"))
            }
            return novaToken.value
        }

    }

    return {
        setToken,
        removeToken,
        getToken,
    }
})

export const loginUrlStore = defineStore('loginUrlStore', () => {
    const loginUrl = ref(null);
    const setLoginUrl = (v) => {
        loginUrl.value = v
        localStorage.setItem("nova-loginUrl", v)
    }
    const getLoginUrl = () => {
        if(null!==loginUrl.value&&"null"!==loginUrl.value){
            return loginUrl.value
        }else {
            if(null!==localStorage.getItem("nova-loginUrl")&&"null"!==localStorage.getItem("nova-loginUrl")){
                setLoginUrl(localStorage.getItem("nova-loginUrl"))
            }else{
                loginUrl.value = "login"
            }
            return loginUrl.value
        }
    }

    return {
        getLoginUrl,
        setLoginUrl,
    }
})

export const useTagList =  defineStore('tags_store', () => {
    const tags = ref([])
    const defaultTags = ref({
        label: "首页",
        path: "/nova/index"
    })
    const tag = ref({})
    const addTag = (item) => {
        if (tags.value.some(ele => ele.path == item.path))
        {
            tag.value = item
        }else{
            tags.value.push(item)
            tag.value = item
        }

    }
    const delTag = (item) => {
        tags.value = tags.value.filter(ele => {
            return ele.path !== item.path
        })
    }
    const delAllTag = ()=>{
        tags.value = []
    }
    const delOtherTag = (v) => {
        if(v){
            tags.value = tags.value.filter(item => {
                return [v.path, defaultTags.value.path].includes(item.path)
            })
        }else{
            tags.value = tags.value.filter(item => {
                return [tag.value.path, defaultTags.value.path].includes(item.path)
            })
        }


    }
    const getTagList = () => {
        if(tags.value.length === 0){
            tags.value.push(defaultTags.value)
        }
        return tags.value
    }
    const getTag = () => {
        if(!tag.value.path){
            return defaultTags.value
        }
        return tag.value
    }

    const setTag = (v) => {
        tag.value = v
    }

    return {
        getTagList,
        addTag,
        getTag,
        delTag,delAllTag,delOtherTag,setTag
    }
})

export const useRouterStore = defineStore('router_store', () => {
    // 存储菜单数据
    const menuData = ref([]); // 根据你的菜单数据结构调整类型

    // 存储动态路由数据
    const dynamicRouters = ref([]);

    // 存储是否已初始化路由的状态
    const isRoutersInitialized = ref(false);

    // 存储上一次页面刷新的路由
    const beforeRouter = ref('');
    // 初始化动态路由
    const setDynamicRoutes = (menu = []) => {
        // 只在未初始化路由时执行
        if (!isRoutersInitialized.value) {
            // 调用 initDynamicRoutes 函数来生成动态路由
            initDynamicRoutes(menu);

            // 将菜单数据存储到状态中
            dynamicRouters.value = menu;

            // 设置已初始化状态
            isRoutersInitialized.value = true;
            const beforeunloadJson = JSON.parse(localStorage.getItem("beforeunload"))

            beforeRouter.value = beforeunloadJson.path
            router.push(beforeRouter.value)
        }

    };

    // 获取动态路由
    const getDynamicRoutes = () => {

        if(dynamicRouters.value.length===0){
            getNav().then((res)=>{
                setDynamicRoutes(res.data.data)
            })

        }
        return dynamicRouters.value;
    };


    // 更新动态路由
    const setUpdatedDynamicRoutes = (routes = []) => {
        dynamicRouters.value = routes;
    };

    const getBeforeRoute = ()=>{
        return beforeRouter.value
    }

    return {
        isRoutersInitialized, // 公开这个状态，方便其他地方判断
        setDynamicRoutes,
        getDynamicRoutes,
        setUpdatedDynamicRoutes, // 更新动态路由的函数
        getBeforeRoute
    };
})

export const useUserInfoStore = defineStore('user_user_info_store', () => {
    const userInfo = ref([])
    const setUserInfo = (v) => {
        userInfo.value = v
    }
    const getUserInfo = () => {
        return userInfo.value
    }
    return {
        getUserInfo,
        setUserInfo,
    }
})